package com.wudi.estate.controller.tolladministration;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.tolladministration.Charges;
import com.wudi.estate.bean.tolladministration.PayItems;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.homemanage.BuildingsService;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import com.wudi.estate.service.tolladministration.ChargesService;
import com.wudi.estate.service.tolladministration.PayItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.*;

@SessionAttributes(value = {"persAndBuidings","charges1"})
@RequestMapping("payItems")
@Controller
public class PayItemsController {
    @Autowired
    private PayItemsService payItemsService;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private ChargesService chargesService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private BuildingsService buildingsService;

    @RequestMapping("/listUi")
    private String payItems_listUi(String chid,ModelMap map){
        List<Map<String,Object>> persAndBuidings =new ArrayList<>();
        try {
            //放入session域中，为添加做准备
            List<Charges> charges1 = chargesService.selectAll();
            List<Personnel> personnelList=personnelService.selectAll();
            for (Personnel personnel:personnelList){
                Map<String,Object> m = new HashMap<>();
                Buildings buildings = new Buildings();
                buildings.setId(personnel.getBid());
                Buildings buildings1 = buildingsService.selectById(buildings);
                m.put("personnel",personnel);
                m.put("building",buildings1);
                Community community1 = communityService.selectById(buildings1.getCid());
                m.put("community",community1);
                persAndBuidings.add(m);
            }
            map.addAttribute("charges1",charges1);
            map.addAttribute("persAndBuidings",persAndBuidings);
            //为list准备
            if (chid==null){
                List<PayItems> payItems =payItemsService.selectAll();
                List<Map<String, Object>> perPayItemsAndCharges = insertList(payItems);
                map.addAttribute("perPayItemsAndCharges",perPayItemsAndCharges);
            }
            else {
                List<PayItems> payItems =payItemsService.selectByName(chid);
                List<Map<String, Object>> perPayItemsAndCharges = insertList(payItems);
                map.addAttribute("perPayItemsAndCharges",perPayItemsAndCharges);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "payItems_list";
    }
    public List<Map<String,Object>> insertList(List<PayItems> payItems) throws Exception {
        List<Map<String,Object>> perPayItemsAndCharges = new ArrayList<>();
        for (PayItems p: payItems) {
            Map<String,Object> m = new HashMap<>();
            Personnel per = new Personnel();
            per.setId(p.getPid());
            Personnel personnel = personnelService.selectById(per);
            Buildings buildings = new Buildings();
            buildings.setId(personnel.getBid());
            Buildings buildings1 = buildingsService.selectById(buildings);
            Community community = communityService.selectById(buildings1.getCid());

            Charges c =new Charges();
            c.setId(p.getChid());
            Charges charges = chargesService.selectById(c);
            m.put("payItems",p);
            m.put("community",community);
            m.put("personnel",personnel);
            m.put("charge",charges);
            perPayItemsAndCharges.add(m);
        }
        return perPayItemsAndCharges;
    }
    @RequestMapping("/addUi")
    private String payItems_addUi(){
        return "payItems_add";
    }
    @RequestMapping("/add")
    private String payItems_add(PayItems payItems){
        payItems.setCreateTime(new Date());
        payItems.setState(true);
        try {
            payItemsService.insert(payItems);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/payItems/listUi";
    }
    @RequestMapping("/editUi")
    private  String  payItems_editUi(long id,ModelMap map){
        PayItems p = new PayItems();
        p.setId(id);
        PayItems payItems = new PayItems();
        try {
            payItems = payItemsService.selectById(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("payItems",payItems);
        return "payItems_edit";
    }
    @RequestMapping("/edit")
    private String patItems_edit(PayItems payItems){
        payItems.setCreateTime(new Date());
        try {
            payItemsService.updateById(payItems);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/payItems/listUi";
    }
    @RequestMapping("/update")
    private String patItems_edit(long id){
        PayItems p = new PayItems();
        p.setId(id);
        try {
            payItemsService.updateStateById(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/payItems/listUi";
    }
}
